// pages/index/mediationroom/add_reserve/add_reserve.js
import Toast from "../../../../miniprogram_npm/@vant/weapp/toast/toast";
import Dialog from "../../../../miniprogram_npm/@vant/weapp/dialog/dialog";

const utils = require("../../../../utils/util.js")
const app = getApp()
const log = new app.appLogs()
const http = new app.appHttp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //主题
    theme: null,
    //调解室类型
    type: null,
    typeList: [],
    typeShow: false,
    //调解员
    mediationPeople: null,
    mediationPeopleShow: false,
    mediationPeopleList: [],
    //开始时间
    startCurrentTime: new Date().getTime(),
    startMinDate: new Date().getTime(),
    startMaxDate: new Date().getTime(),
    startTime: null,
    startTimeShow: false,
    startTimeText: null,
    //结束时间
    endMinDate: new Date().getTime(),
    endMaxDate: new Date().getTime(),
    endTime: null,
    endTimeShow: false,
    endTimeText: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //初始化时间
    let nowDate = new Date()
    let startMaxDate = new Date(nowDate.getFullYear(),12,30)
    let endMaxDate =  new Date(startMaxDate.getFullYear(),12,30)
    this.setData({
      startMaxDate: startMaxDate.getTime(),
      endMaxDate: endMaxDate.getTime()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  /**
   * 主题输入框
   * @param e
   */
  onInputTheme: function (e) {
    this.setData({
      theme: e.detail.value
    })
  },

  /**
   * 关闭弹窗
   * @param e
   */
  onClose: function (e) {
    this.setData({
      typeShow: false, //调解室类型
      mediationPeopleShow: false, //调解员
      startTimeShow: false, //开始时间
      endTimeShow: false, //结束时间
    })
  },

  /**
   * 调解室类型
   * @param e
   */
  onOrderType: function (e) {
    if (this.data.typeList.length <= 0) {
      this.getType()
    } else {
      this.setData({
        typeShow: true
      })
    }
  },

  /**
   * 调解室类型选择
   * @param e
   */
  onTypeSelect: function (e) {
    this.setData({
      typeShow: false,
      type: e.detail
    })
  },

  /**
   * 调解员
   * @param e
   */
  onMediationPeople: function (e) {
    if (this.data.mediationPeopleList.length <= 0) {
      this.getMediationPeople()
    } else {
      this.setData({
        mediationPeopleShow: true
      })
    }
  },
  onMediationPeopleSelect: function (e) {
    this.setData({
      mediationPeople: e.detail,
      mediationPeopleShow: false
    })
  },


  /**
   * 开始时间
   * @param e
   */
  onStartTime: function (e) {
    this.setData({
      startTimeShow: true
    })
  },
  onStartTimeConfirm: function (e) {
    let startDate = new Date(e.detail)
    this.setData({
      startTime: e.detail,
      startTimeShow: false,
      endMinDate: startDate.getTime(),
      endMaxDate: new Date(startDate.getFullYear() + 1,12,30).getTime(),
      startTimeText: utils.formatDate(e.detail / 1000,"Y-M-D h:m")
    })
  },

  /**
   * 结束时间
   * @param e
   */
  onEndTime: function (e) {
    if (this.data.startTime == null) {
      Toast.fail("请先选择开始时间")
      return
    }
    this.setData({
      endTimeShow: true,
    })
  },
  onEndTimeConfirm: function (e) {
    this.setData({
      endTime: e.detail,
      endTimeShow: false,
      endTimeText: utils.formatDate(e.detail / 1000,"Y-M-D h:m")
    })
  },

  /**
   * 获取调解室类型
   */
  getType: function () {
    Toast.loading({
      duration: 0,
      message: "加载中..."
    })
    http.get(app.globalData.xw_system_url + "dic/getDicListByName/open",{
      name: "D0064"
    }).then(res => {
      if (res.data.code === http.REQUEST_OK) {
        Toast.clear()
        res.data.data.forEach(item => {
          item.name = item.dic_value
        })
        this.setData({
          typeList: res.data.data,
          typeShow: true
        })
      } else {
        Toast.fail(res.data.msg)
      }
    }, err => {
      Toast.fail("获取数据失败")
    })
  },

  /**
   * 获取调解员列表
   */
  getMediationPeople: function () {
    Toast.loading({
      duration: 0,
      message: "加载中..."
    })
    http.get(app.globalData.xw_api_url + "mediate/information/getZzPeopleList",{
      area_code: app.globalData.areaCode,
      people_id: app.globalData.userInfo.people_id
    }).then(res => {
      if (res.data.code === http.REQUEST_OK) {
        Toast.clear()
        this.setData({
          mediationPeopleList: res.data.data,
          mediationPeopleShow: true
        })
      } else {
        Toast.fail(res.data.msg)
      }
    }, err => {
      Toast.fail("获取调解员列表失败")
    })
  },

  /**
   * 提交按钮
   * @param e
   */
  onSubmit: function (e) {
    if (this.data.type == null) {
      Toast.fail("请选择调解室类型")
      return;
    }

    Toast.loading({
      duration: 0,
      message: "正在提交..."
    })
    http.postJson(app.globalData.xw_api_url + "mediate/information/add",{
      area_code: app.globalData.areaCode,
      people_id: app.globalData.userInfo.people_id,
      make_user_id: app.globalData.userInfo.applet_user_id,
      make_user_name: app.globalData.userInfo.nickname,
      status: 0,
      on_type: 1,
      mediate_type: 1,
      make_status: 1, //（0.驳回1.待受理2.受理）
      type: this.data.type.flag,
      //主题
      theme: this.data.theme,
      mediate_name:  this.data.theme,
      //工作人员id
      create_id: this.data.mediationPeople.id,
      //工作人员名称
      create_name: this.data.mediationPeople.name,
      //开始时间
      start_time: this.data.startTimeText + ":00",
      //结束时间
      end_time: this.data.endTimeText + ":00"
    }).then(res => {
      if (res.data.code === http.REQUEST_OK) {
        Toast.success("提交成功")
        wx.navigateBack()
      } else {
        Toast.clear()
        Dialog.alert({
          title: "提交失败",
          message: res.data.msg
        }).then(() => {  })
      }
    }, err => {
      Toast.clear()
      Dialog.alert({
        title: "提交失败",
        message: "网络异常，请稍后再试！"
      }).then(() => {  })
    })
  },

})